package edu.sctu.lab03;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sql = "UPDATE t_users " +
                "SET  username = '"+username+"',password='"+password+"'" +
                "WHERE id="+id+"";
        try {
            Connection connection = null;
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
                    "root",
                    "123456"
            );
            Statement statement = connection.createStatement();
            statement.execute(sql);
            statement.close();
            connection.close();
            resp.sendRedirect("http://localhost:8080/userList");

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
